job "prometheus" {
  datacenters = ["dc1"]
  type        = "service"

  group "server" {
    count = 1

    network {
      port "http" {
        static = 9090
      }
    }

    service {
      name = "prometheus"
      tags = ["monitoring", "metrics"]
      port = "http"

      check {
        type     = "http"
        path     = "/-/healthy"
        interval = "10s"
        timeout  = "2s"
      }
    }

    task "prometheus" {
      driver = "docker"

      config {
        image = "prom/prometheus:latest"
        ports = ["http"]

        volumes = [
          "local/prometheus.yml:/etc/prometheus/prometheus.yml",
          "prometheus-data:/prometheus"
        ]
      }

      resources {
        cpu    = 500
        memory = 1024
      }

      service_registration "nomad" {}

      template {
        data = <<EOF
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'nomad'
    consul_sd_configs:
      - server: '${NOMAD_PORT_http}'
        services: []
    relabel_configs:
      - source_labels: [__meta_consul_service]
        regex:         (.*)
        target_label:  job
        replacement:   ${1}
      - source_labels: [__meta_consul_service_metadata_scrape]
        regex:         true
        action:        keep
EOF
        destination = "local/prometheus.yml"
      }
    }
  }
}